/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaCentros.java
 *
 * Created on May 29, 2012, 11:50:34 AM
 */
package br.ufrn.adurn.gui;

import br.ufrn.adurn.architecture.dao.CentroDAO;
import br.ufrn.adurn.domain.Centro;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Victor
 */
public class TelaCentros extends javax.swing.JFrame {

    DefaultTableModel model;

    /** Creates new form TelaCentros */
    public TelaCentros() {
        initComponents();

        centrosTable.getTableHeader().setReorderingAllowed(false);
        initTable();
    }

    private void initTable() {
        CentroDAO dao = new CentroDAO();
        List<Centro> centros = dao.getLista();

        String[] str = new String[]{"Sigla", "E-mail", "Telefone 1", "Telefone 2"};

        int strLength = str.length;

        Object[][] obj = new Object[centros.size()][strLength];

        for (int i = 0; i < centros.size(); i++) {
            obj[i][0] = String.valueOf(centros.get(i).getSigla());
            obj[i][1] = String.valueOf(centros.get(i).getEmail());
            obj[i][2] = String.valueOf(centros.get(i).getTel1());
            obj[i][3] = String.valueOf(centros.get(i).getTel2());

        }

        model = new DefaultTableModel(obj, str) {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        centrosTable.setModel(model);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        centrosTable = new javax.swing.JTable();
        voltarButton = new javax.swing.JButton();
        removerButton = new javax.swing.JButton();
        novoButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SysADURN");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(" Centros cadastrados "));

        centrosTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Sigla", "E-mail", "Telefone 1", "Telefone 2"
            }
        ));
        jScrollPane1.setViewportView(centrosTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
        );

        voltarButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\Victor\\Documents\\NetBeansProjects\\SysADURN\\icons\\arrow_left.png")); // NOI18N
        voltarButton.setText("Voltar");

        removerButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\Victor\\Documents\\NetBeansProjects\\SysADURN\\icons\\table_delete.png")); // NOI18N
        removerButton.setText("Remover");
        removerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removerButtonActionPerformed(evt);
            }
        });

        novoButton.setIcon(new javax.swing.ImageIcon("C:\\Users\\Victor\\Documents\\NetBeansProjects\\SysADURN\\icons\\table_add.png")); // NOI18N
        novoButton.setText("Novo...");
        novoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novoButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(voltarButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removerButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(novoButton))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(novoButton)
                    .addComponent(removerButton)
                    .addComponent(voltarButton))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void novoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novoButtonActionPerformed
        new TelaCadastrarCentro().setVisible(true);

    }//GEN-LAST:event_novoButtonActionPerformed

    private void removerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removerButtonActionPerformed
        CentroDAO dao = new CentroDAO();
        List<Centro> centros = dao.getLista();
        Centro centroSelecionado = centros.get(centrosTable.getSelectedRow());
        dao.remove(centroSelecionado);
        System.out.println(centroSelecionado.getSigla() + " removido com sucesso!");
    }//GEN-LAST:event_removerButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable centrosTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton novoButton;
    private javax.swing.JButton removerButton;
    private javax.swing.JButton voltarButton;
    // End of variables declaration//GEN-END:variables
}
